package com.nd.mobile.study.common;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Application;

import com.nd.mobile.study.common.constants.Tips;
import com.nd.mobile.study.common.exception.LocalErrorException;
import com.nd.mobile.study.domain.User;

public class App extends Application {

	private static App	instance;

	public static int		screenWidth;

	public static App getInstance() {
		return instance;
	}

	private String							tabTag		= "";

	private User							mUser;

	private List<WeakReference<Activity>>	activities	= new ArrayList<WeakReference<Activity>>();

	public String getTabTag() {
		return tabTag;
	}

	/**
	 * 判断是否有用户登录
	 * @return
	 */
	public boolean hasUserLogin(){
		return null!= mUser;
	}
	
	public User getUser() throws LocalErrorException {
		if (null == mUser) {
			throw new LocalErrorException(Tips.ERROR_ON_NULL_USER);
		}
		return mUser;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
	}

	public void setTabTag(String tabTag) {
		this.tabTag = tabTag;
	}

	public void setUser(User user) {
		mUser = user;
	}

	public void registActivity(Activity activity) {
		activities.add(new WeakReference<Activity>(activity));
	}

	public void finishActivities() {
		for (WeakReference<Activity> actitity : activities) {
			if (null != actitity.get()) {
				actitity.get().finish();
			}
		}
	}
}
